package com.taoban.shopdata.core.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.taoban.shopdata.core.AbstractShopDataConsumer;
import com.taoban.shopdata.entity.PriceUpdatedCommitResult;
import com.taoban.shopdata.thread.YHDProductCollectThread;
import com.taoban.shopdata.utils.Constants;
import com.taoban.shopdata.utils.FactoryUtils;
import com.yufei.utils.ExceptionUtil;

public class YHDShopDataConsumer extends AbstractShopDataConsumer<String,Double> {
	private static Log mLog = LogFactory.getLog(YHDShopDataConsumer.class);
	public  static  Map<String,Double> productPriceMap=new HashedMap();
	public YHDShopDataConsumer(PriceUpdatedCommitResult priceUpdatedCommitResult) {
		super();
		this.priceUpdatedCommitResult=priceUpdatedCommitResult;
		this.itemInfos=priceUpdatedCommitResult.getItemInfos();
		// TODO Auto-generated constructor stub
	}
	/*@Override
	public void commitItemPrice() {
		// TODO Auto-generated method stub
		 mLog.info("开始提交价格更行信息，时间为:"+DateUtil.getCurrentDateString()+"");
	        mLog.info("此次提交更行的商品个数为:"+itemInfos.size()+"");
			 DataRetrieverFeatures dataRetrieverFeatures = new DataRetrieverFeatures(false, "post");
			 dataRetrieverFeatures.setRequestTimeout(1000*60);
			HttpDataRetriever dataRetriever = DataRetrieverFactory
						.createDataRetriever(dataRetrieverFeatures);
			try {
				priceUpdatedCommitResult.setItemInfos(itemInfos);
				priceUpdatedCommitResult.setPrices(prices);
			    String content=CommonUtil.generateXmlForObject(priceUpdatedCommitResult, PriceUpdatedCommitResult.class);
			    String urlForCommitUpdatedPrice = null;
		        mLog.debug("此次提交的价格更行的内容是:"+content+",commitUrl="+urlForCommitUpdatedPrice+"");
		    try {
		    	//Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml")
				urlForCommitUpdatedPrice=ConfigUtils.getUrlForCommitUpdatedPrice();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				mLog.info("read the config.xml Error for get urlForCommitUpdatedPrice!");
				return;
			} 
		    
			dataRetriever.setUrl(new URL(urlForCommitUpdatedPrice));
			List<BasicNameValuePair> formParams=new ArrayList<BasicNameValuePair>();
			formParams.add(new BasicNameValuePair("action", "commitUpdatedPrice"));

			formParams.add(new BasicNameValuePair("xmlContent", content));
			dataRetriever.setFormParams(formParams);
			dataRetriever.connect();
			
				mLog.info(dataRetriever.getHtmlContent());
				dataRetriever.disconnect();
		        mLog.info("提交价格更行信息成功，时间为:"+DateUtil.getCurrentDateString()+"");


			} catch (IOException e) {
				// TODO Auto-generated catch block
				mLog.info("提交价格更行信息失败,具体异常信息是:"+ExceptionUtil.getExceptionDetailsMessage(e)+"");
				throw new RuntimeException(e);
			}
		}*/

		@Override
		public List<Double> getItemPriceByItemInfo(List<String> itemInfo) {
		
			return null;
			// TODO Auto-generated method stub


	}

	@Override
	public void process() {
		// TODO Auto-generated method stub
		if(productPriceMap.size()==0){
			try {
				Thread.currentThread().sleep(1000*60*30);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
			mLog.info(""+ExceptionUtil.getExceptionDetailsMessage(e)+"");
			}
		}
		Double tempPrice=null;
		List<String> tempItemInfos=itemInfos;
		itemInfos=new ArrayList<>();
		for(String itemId:tempItemInfos){
			tempPrice=productPriceMap.get(itemId);
		    if(tempPrice==null){
		    	
		    	continue;
		    }
		    itemInfos.add(itemId);
		    prices.add(tempPrice);
		}
		this.commitItemPrice();
	}



public static void main(String[] args){
	new Thread(new YHDProductCollectThread()).run();
	List<String> itemInfos=new ArrayList<>();
	itemInfos.add("1873040");
	itemInfos.add("1873042");

	itemInfos.add("1873043");
    
	
	PriceUpdatedCommitResult priceUpdatedCommitResult=new PriceUpdatedCommitResult(Constants.yihaodian, itemInfos);
	try{
		AbstractShopDataConsumer abstractShopDataConsumer=FactoryUtils.getShopDataConsumer(priceUpdatedCommitResult);
		abstractShopDataConsumer.process();
	}catch(Exception e){
		mLog.info("错误信息是:"+e.getMessage()+"");
	}
	
}

}
